package com.qianfeng.zhangwei.fruitday.ui;


import android.support.v4.app.Fragment;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.qianfeng.zhangwei.fruitday.BaseApp;
import com.qianfeng.zhangwei.fruitday.R;
import com.qianfeng.zhangwei.fruitday.fragment.FruitFragment_;
import com.qianfeng.zhangwei.fruitday.fragment.GoodTypeFragment_;
import com.qianfeng.zhangwei.fruitday.fragment.HomeFragment_;
import com.qianfeng.zhangwei.fruitday.fragment.MyGardenFragment_;
import com.qianfeng.zhangwei.fruitday.fragment.ShopCartFragment_;
import com.qianfeng.zhangwei.fruitday.utils.AppManager;
import com.qianfeng.zhangwei.fruitday.utils.FragmentTabUtils;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseAcitivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {
    private long firstTime;
    @ViewById(R.id.main_rgs)
    RadioGroup rgs;
    private FragmentTabUtils tabUtils;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    @App
    BaseApp app;

    @Override
    public void injectBefore() {
        fragments.add(new HomeFragment_());
        fragments.add(new GoodTypeFragment_());
        fragments.add(new MyGardenFragment_());
        fragments.add(new ShopCartFragment_());
        fragments.add(new FruitFragment_());
    }

    @Override
    public void initView() {
        tabUtils = new FragmentTabUtils(getSupportFragmentManager(), fragments, R.id.fragment_container, rgs);
        tabUtils.setOnRgsExtraCheckedChangedListener(this);
    }


    /**
     * 返回监听
     */
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2 * 1000) {//如果两次按键时间间隔大于800毫秒，则不退出
            Toast.makeText(MainActivity.this, "再按一次退出程序...", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
        } else {
            //注销网络监听
            app.unregisterNetworkStateReceiver();
            //退出程序
            AppManager.getAppManager().AppExit(app);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

    }

    //
//    @Override
//    public void initData() {
//        HttpUtils httpUtils = new HttpUtils();
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("sign", "b18baf872d1c1a80018e139dd5a93525");
//        params.addBodyParameter("limit", "20");
//        params.addBodyParameter("timestamp", "1438940967");
//        params.addBodyParameter("platform", "ANDROID");
//        params.addBodyParameter("source", "app");
//        params.addBodyParameter("page", "3");
//        params.addBodyParameter("connect_id", "");
//        params.addBodyParameter("region_id", "143949");
//        params.addBodyParameter("service", "fruit.getMaxArticleList");
//        params.addBodyParameter("device_id", "93789e8978f2eefdcf52006b4d96ee23");
//        params.addBodyParameter("channel", "qq");
//        params.addBodyParameter("version", "2.3.0");
//
//
//        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                Log.e("responseInfo", responseInfo.result);
//            }
//
//            @Override
//            public void onFailure(HttpException e, String s) {
//
//            }
//        });

//    }
}
